﻿/************************************************************************************
 * 漠然(459699936@qq.com) 2015/1/9 17:55:05
 ************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Justin.OSGI
{
    /// <summary>
    /// 组件模块 对应的是一个物理dll组
    /// </summary>
    public class Module
    {

        public Module(
            string moduleName = "模块名字",
            string description = "模块描述",
            Version version = null,
            ModuleStartPolicy policy = ModuleStartPolicy.ModuleStartAtInstance) {

            this.cache = new Dictionary<string, ModuleCell>();
            this.State = ModuleState.Unknown;
            this.ModuleName = moduleName;
            this.ModuleDescription = description;
            this.ModuleVersion = version;
            this.StartPolicy = policy;
        }
        /// <summary>
        /// 用于存储模块里面的描述信息
        /// ModuleCell用于描述 插件信息
        /// key 用于分配处理器和管理程序
        /// </summary>
        private Dictionary<string, ModuleCell> cache;
        /// <summary>
        /// 外部只能读取
        /// 而要修改内容则要通过管理函数接口进行
        /// </summary>
        public IReadOnlyDictionary<string, ModuleCell> ModuleCellCache {
            get {
                return this.cache;
            }
        }
        /// <summary>
        /// 添加模块功能单元
        /// </summary>
        /// <param name="key"></param>
        /// <param name="cell"></param>
        /// <returns></returns>
        public bool AddModuleCell(string key, ModuleCell cell) {
            lock (this.cache) {
                if (!this.cache.ContainsKey(key)) {
                    this.cache.Add(key, cell);
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 模块的状态
        /// </summary>
        public ModuleState State { private set; get; }
        /// <summary>
        /// 设置模块的状态
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public ModuleState SetModuleState(ModuleState state) {
            this.State = State;
            return this.State;
        }
        /// <summary>
        /// 启动方案
        /// 如果要修改,则要重新存储这个方案
        /// </summary>
        public ModuleStartPolicy StartPolicy { private set; get; }
        /// <summary>
        /// 模块名
        /// </summary>
        public string ModuleName { private set; get; }
        /// <summary>
        /// 模块描述
        /// </summary>
        public string ModuleDescription { private set; get; }

        /// <summary>
        /// 模块的版本号
        /// </summary>
        public Version ModuleVersion { get; private set; }
    }
}
